<?php
/**
 * Created by JetBrains PhpStorm.
 * User: thanh
 * Date: 4/12/12
 * Time: 8:27 AM
 * To change this template use File | Settings | File Templates.
 */
class VideoController extends Controller
{
    public $layout = 'column3';

    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('index','view'),
                'users'=>array('*'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions'=>array('rate'),
                'roles'=>array('register'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

    public function actionIndex()
    {
        $criteria = new CDbCriteria(array(
            'condition' => 'status=1',
            'order' => 'create_time DESC',
            //'with'=>'commentCount',
        ));
        if(isset($_GET['cid']))
            $criteria->addSearchCondition('cat_id',$_GET['cid']);

        $dataProvider = new CActiveDataProvider('Video', array(
            'pagination' => array(
                'pageSize' => Yii::app()->params['postsPerPage'],
            ),
            'criteria' => $criteria,
        ));
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionView($vid)
    {
        $video = Video::model()->findByPk($vid);
        $video->view++;
        $video->update(array('view'));
        $comment = $this->newComment($video);
        $this->render('view', array('model' => $video, 'comment' => $comment));
    }

    public function actionRate($id)
    {
        $video = Video::model()->findByPk($id);
        if (Yii::app()->user->isGuest) {
            echo json_encode(array('login' => 1));
            exit;
        }
        if (isset($_POST) && $_POST['marks']) {
            if (!$video->ownrateCount) {
                $rate = new VideoRate();
                $rate->user_id = Yii::app()->user->id;
                $rate->video_id = $video->id;
                $rate->rate_time = time();
                $rate->marks = (int)$_POST['marks'];
                if ($rate->save()) {
                    /*$video->refresh();
                    $ok = array('success'=>1,'star'=>'','av'=>' ('.$video->averrate.', '.$video->rateCount.')');
                    if($video->star){
                        for($i = 0;$i < $video->star;$i++){
                            $ok['star'] .= '<img src="/images/star.png" />';
                        }
                    }*/
                    header('Content-Type:application/json');
                    echo json_encode(array('success' => 1));
                }

            }
        }
    }

    /**
     * @param $video Training
     * @return Comments
     */
    protected function newComment($video)
    {
        $comment = new Comments;
        if (!Yii::app()->user->isGuest) {
            $comment->user_id = (int)Yii::app()->user->id;
            //            $comment->item_type = 'training';
            //$comment->item_id = $trainig->id;
        }
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'comments-form') {
            echo CActiveForm::validate($comment);
            Yii::app()->end();
        }
        if (isset($_POST['Comments'])) {
            $comment->attributes = $_POST['Comments'];
            if ($video->addComment($comment)) {
                Yii::app()->user->setFlash('commentSubmitted', 'Cảm ơn bạn đã bình luận.');
                $this->refresh();
            }
        }
        return $comment;
    }

}
